package com.zyg.shop.service.impl;

import com.alibaba.fastjson.JSON;
import com.zyg.shop.entity.GoodsEntity;
import com.zyg.shop.entity.SpecificationOptionEntity;
import com.zyg.shop.entity.TypeTemplateEntity;
import com.zyg.shop.entity.group.Goods;
import com.zyg.shop.entity.vo.SpecOptionsVo;
import com.zyg.shop.service.SpecificationOptionService;
import com.zyg.shop.service.TypeTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zyg.common.utils.PageUtils;
import com.zyg.common.utils.Query;

import com.zyg.shop.dao.SpecificationDao;
import com.zyg.shop.entity.SpecificationEntity;
import com.zyg.shop.service.SpecificationService;


@Service("specificationService")
public class SpecificationServiceImpl extends ServiceImpl<SpecificationDao, SpecificationEntity> implements SpecificationService {


    @Autowired
    private TypeTemplateService templateService;
    @Autowired
    private SpecificationOptionService specOptionService;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SpecificationEntity> page = this.page(
                new Query<SpecificationEntity>().getPage(params),
                new QueryWrapper<SpecificationEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 作者：LMY
     * 功能：根据模板Id查出SpecIds,再根据SpecIds查询出相关规格选项
     * 时间：2021/10/29 1:08
     */
    @Override
    public List<SpecOptionsVo> findSpecList(Long id) {
        //1. 通过模板id查询出模板对象
        TypeTemplateEntity template = templateService.getById(id);
        //2. 拿到specIds
        String specIds = template.getSpecIds();
        //3. 转换成List<SpecOptionsVo> 集合
        List<SpecOptionsVo> specList = JSON.parseArray(specIds,SpecOptionsVo.class);
        //4. 遍历集合
        for (SpecOptionsVo specOptionsVo : specList) {
            //4.1 拿到规格Id
            Long specId = specOptionsVo.getId();
            //4.2 根据规格Id查询对应的规格选项集合,查询条件 外键id
            List<SpecificationOptionEntity> options= specOptionService.list(new QueryWrapper<SpecificationOptionEntity>().eq("spec_id", specId));
            //4.3 存储到转换成List<SpecOptionsVo> 中
            specOptionsVo.setOptions(options);
        }
        //5. 返回
        return specList;
    }



}